package dnablockfinder;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public abstract class EvoNode {
	protected double evoLength;

	public static EvoNode factory
			(String path, Map<String,String> sequences) throws IOException {
		String parseString = Files
			.readAllLines(Paths.get(path), Charset.forName("US-ASCII")).get(0);
		EvoNode retNode = factory(parseString, 0);
		retNode.parse(parseString, 0, ';', sequences);
		return retNode;
	}

	public abstract Analysis[] analyze(int filterRadius);
	public abstract String print(Path path) throws IOException;

	protected static EvoNode factory(String parseString, int index) {
		return parseString.charAt(index) == '('
			? new EvoBranch() : new EvoLeaf();
	}

	protected final int parse(String parseString, int index,
			char endTag, Map<String,String> sequences) {
		index = parseInternal(parseString, index, endTag, sequences) + 1;
		int eoo = parseString.indexOf(endTag, index);
		if (eoo >= 0)
			evoLength = Double.parseDouble(parseString.substring(index, eoo));
		return eoo;
	}

	protected abstract int parseInternal(String parseString, int index,
			char endTag, Map<String,String> sequences);
}
